package com.easyUploader.service.impl;


import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.mybaits.CustomPage;
import com.easyUploader.assembler.BucketAssembler;
import com.easyUploader.entity.dao.Bucket;
import com.easyUploader.entity.dao.FileStorage;
import com.easyUploader.entity.dto.BucketDto;
import com.easyUploader.entity.dto.FileDto;
import com.easyUploader.entity.vo.BucketVo;
import com.easyUploader.mapper.BucketMapper;
import com.easyUploader.property.LocalFileProperty;
import com.easyUploader.service.IBucketService;
import com.easyUploader.service.IFileStorageService;
import com.easyUploader.threadHolder.UserThreadHolder;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

import static com.easyUploader.entity.factory.BucketFactory.BUCKET_CONVERTER;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 大忽悠
 * @since 2022年11月01日
 */
@Service
@RequiredArgsConstructor
public class BucketServiceImpl extends ServiceImpl<BucketMapper, Bucket> implements IBucketService {
    private final BucketAssembler bucketAssembler;
    private final LocalFileProperty localFileProperty;

    @Override
    public Boolean create(BucketVo bucketVo) {
        bucketVo.safeCheck(this);
        Bucket bucket = BUCKET_CONVERTER.toBucket(bucketVo);
        bucket.setCreateBy(UserThreadHolder.get().getId());
        bucket.setUpdateBy(UserThreadHolder.get().getId());
        bucket.setPath(SpringUtil.getBean(LocalFileProperty.class).getUserBucketRelativePath(bucket.getName()));
        return save(bucket);
    }

    @Override
    public List<BucketDto> list(CustomPage customPage) {
        return bucketAssembler.assembler(CustomPage.getResult(customPage, new Bucket(), this,
                //只查询当前用户拥有的存储桶
                new QueryWrapper<Bucket>().eq("create_by",UserThreadHolder.get().getId())));
    }


    @Transactional
    @Override
    public Boolean delete(Integer id) {
        Bucket bucket = getById(id);
        bucket.deleteCheck();
        clearFilesUnderBucket(id);
        removeById(id);
        return true;
    }

    @Override
    public Bucket getBucketByName(String bucket) {
        bucket=BucketVo.getDefaultIfNeeded(bucket);
        Bucket bucketOne = getOne(new QueryWrapper<Bucket>().eq("name", bucket).eq("create_by",UserThreadHolder.get().getId()));
        Assert.notNull(bucketOne, "名为" + bucket + "的存储桶不存在");
        return bucketOne;
    }

    @Override
    public List<FileDto> listFileInfo(String bucketName, String dir, CustomPage customPage) {
        Bucket bucket = getBucketByName(bucketName);
        return BucketDto.listFileInfo(localFileProperty,bucket.getName(),dir,customPage);
    }

    @Override
    public Boolean setBucketType(Integer id, Integer type) {
        Bucket bucket = Bucket.builder().bucketType(type).build();
        return update(bucket,new QueryWrapper<Bucket>().eq("id",id).eq("create_by",UserThreadHolder.get().getId()));
    }

    private void clearFilesUnderBucket(Integer id) {
        Bucket bucket = getById(id);
        //删除bucket目录--目录不为空也删除
        bucket.doDeleteFiles(localFileProperty);
        SpringUtil.getBean(IFileStorageService.class).remove(new QueryWrapper<FileStorage>().eq("bucket_id",id));
    }
}
